package org.mech.sefer.client.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.mech.sefer.model.bible.TBible;
import org.mech.sefer.model.bible.TBook;
import org.mech.sefer.service.bible.BibleService;
import org.springframework.stereotype.Service;

@Service("bibleService")
public class BibleServiceMock implements BibleService {
	
	@Override
	public List<TBible> loadAll() {
		
		List<TBible> bibles = new ArrayList<TBible>();
		
		try {
			File file = new File("src/test/resources/data/rohacek.data");
		    
		    String fileToString = FileUtils.readFileToString(file);
		    
		    String[] lines = fileToString.split("\n");
		    
		    TBible bible = new TBible();
		    
		    String[] bArray = lines[0].split(" ", 2 );
		    
			bible.setId(0);
			bible.setName(bArray[1]);
			List<TBook> books = new ArrayList<TBook>();
			
		    for(int i = 1; i< lines.length;i++){
		    	String[] array = lines[i].split(" ", 5 );
		    	TBook book = new TBook();
				book.setId("id" + array[1]);
				book.setAbbreviation(array[3]);
				book.setName(array[4]);
				book.setOldTestament(Integer.valueOf(array[1]) < 40);
				book.setSemanticId(Integer.toHexString(Integer.valueOf(array[0])));
				books.add(book);
		    }
		    
		    bible.setBooks(books);
			bibles.add(bible);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return bibles;
	}

}
